import pymysql
from faker import Faker

def insertInfo():
    # 使用faker生成100条数据插入
    data = []
    faker = Faker(locale='zh_CN')
    for i in range(100):
        username = faker.user_name()
        password = 123
        email = faker.email()
        sex = faker.random_element(elements=['M', 'F'])
        phone = faker.phone_number()
        addr = faker.address()
        name = faker.name()
        state = faker.random_element(elements=['1','0'])
        code = faker.building_number()
        data.append((username,password,email,sex,phone,addr,name,state,code))
    conn = pymysql.connections.Connection(host="127.0.0.1", port=3306, user="root", passwd="root", db="shop")
    # 定义sql
    sql = "insert into user(username,password,email,sex,phone,addr,name,state,code) values(%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    cursor = conn.cursor()
    # 执行sql
    result = cursor.executemany(sql,data)
    # rows = cursor.fetchall()
    conn.commit()
    conn.close()
insertInfo()